/*******************************************************************************
* @copyright: Shenzhen Hangshun Chip Technology R&D Co., Ltd
* @filename:  main.c
* @brief:     Main program body
* @author:    AE Team
* @version:   V1.0.0/2025-07-16
*             1.Initial version
* @log:
*******************************************************************************/


/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
extern __IO uint32_t TimingDelay;

/* Private function prototypes -----------------------------------------------*/
static void AWUT_Config(void);
static void AWUT_WakeupTimeConfig(void);
static void SYSCLKConfig_STOP(void);
void Delay(__IO uint32_t nTime);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (KEIL_startup_hk32g003.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_hk32g003.c file
     */

  /* Configures the BUTTON_1(KEY1) */
  HK_EVAL_PBInit(BUTTON_1, BUTTON_MODE_EXTI);

  /* Configure LEDs */
  HK_EVAL_LEDInit(LED1);
  HK_EVAL_LEDInit(LED2);
  HK_EVAL_LEDInit(LED3);

  /* SysTick interrupt each 10 ms */
  if (SysTick_Config(SystemCoreClock / 100))
  {
    /* Capture error */
    while (1)
	{
		
	}
  }

  /* AWUT Configuration */
  AWUT_Config();

  /* LED1 On */
  HK_EVAL_LEDOn(LED1);

  while(1)
  {
    /* Insert 5 second delay */
    Delay(500);

    /* Set wakeup time in 5s */
    AWUT_WakeupTimeConfig();

    /* LEDs Off */
    HK_EVAL_LEDOff(LED1);
    HK_EVAL_LEDOff(LED2);
    HK_EVAL_LEDOff(LED3);

    /* Request to enter STOP mode with regulator in low power mode */
    PWR_EnterStopMode(PWR_Regulator_LowPower, PWR_Entry_WFI);

    /* LED1 On */
    HK_EVAL_LEDOn(LED1);

    /* Configures system clock after wake-up from STOP */
    SYSCLKConfig_STOP();
  }
}

/**
  * @brief  Configures the AWUT.
  * @param  None
  * @retval None
  */
static void AWUT_Config(void)
{
  EXTI_InitTypeDef EXTI_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the PWR clock */
  RCC_APBPeriph1ClockCmd(RCC_APBPeriph1_PWR, ENABLE);

  /* Enable the SYSCFG clock */
  RCC_APBPeriph2ClockCmd(RCC_APBPeriph2_SYSCFG, ENABLE);

  /* Enable the AWUT clcok */
  RCC_APBPeriph1ClockCmd(RCC_APBPeriph1_AWU, ENABLE);

  /* Enable the LSI */
  RCC_LSICmd(ENABLE);

  /* Wait till LSI is ready */
  while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
  {
	  
  }

  /* Configure AWUT clock source */
  AWUT_CLKConfig(AWUT_CLK_LSI);

  /* Configure EXTI line 11 (connected to the AWUT event) */
  EXTI_ClearITPendingBit(EXTI_Line11);
  EXTI_InitStructure.EXTI_Line = EXTI_Line11;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);

  /* NVIC configuration */
  NVIC_InitStructure.NVIC_IRQChannel = EXTI11_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

/**
  * @brief  Configures the AWUT time
  * @param  None
  * @retval None
  */
static void AWUT_WakeupTimeConfig(void)
{
  /* Set AWUT time to 5s */
  while(ERROR == AWUT_TimerCounterAndStart(60000 * 5))
  {
	  
  }
}


/**
  * @brief  Configures system clock after wake-up from STOP: enable HSE, PLL
  *         and select PLL as system clock source.
  * @param  None
  * @retval None
  */
static void SYSCLKConfig_STOP(void)
{
  /* After wake-up from STOP reconfigure the system clock */

  /* HSI will be selected as system clock after waku-up from STOP */

  /* Here add your code, example I2C,SPI clock and so on */
}

/**
  * @brief  Inserts a delay time.
  * @param  nTime: specifies the delay time length, with a base of 250 milliseconds.
  * @retval None
  */
void Delay(__IO uint32_t nTime)
{
  TimingDelay = nTime;

  while(TimingDelay != 0)
  {
	  
  }
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif


